package com.exonline.controller.pay;

import com.exonline.commons.dao.IDao;
import com.exonline.commons.utils.TokenUtil;
import com.exonline.commons.utils.TokenUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

/**
 *@desc   (沙箱支付) 前台支付订单信息  跳转到支付宝的支付订单信息页面
 * @author  靳吉瑞
 */
@WebServlet("/font/pay")
public class PayServlet extends HttpServlet {

    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("skippay".equals(method)){
            try {
                this.skipPay(req, resp);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        } else if ("successpay".equals(method)) {
            this.successPay(req, resp);
        }
    }

    /**
     * @desc  支付成功
     * @param req
     * @param resp
     */
    private void successPay(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println(req.getParameter("outtradeno"));

    }

    /**
     * @desc  支付跳转
     * @param req
     * @param resp
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    private void skipPay(HttpServletRequest req, HttpServletResponse resp) throws IOException, NoSuchAlgorithmException {

        String cid = req.getParameter("cid");
        String sid = req.getParameter("sid");
        String payToken = TokenUtil.createPayToken(sid, cid);
//      String sid = TokenUtil.getId(req);  //根据token获取用户id
        req.setAttribute("sid", sid);
        req.setAttribute("cid", cid);
        //跳转到支付页面
        resp.sendRedirect("http://36.235.128.117:36815/EXOnline/admin/pay" + "?info=" + payToken);
    }
}
